import { _decorator, Button, Component, director, Node, resources } from 'cc';
import { UIManager } from './UIManager';
const { ccclass, property } = _decorator;

@ccclass('UIGame')
export class UIGame extends Component {
    @property(Node)
    private exitBtn: Node = null!;

    @property(Node)
    private settingBtn: Node = null!;

    @property(Node)
    private pauseBtn: Node = null!;

    private findNodeByName(root: Node, name: string): Node | null {
        if (root.name === name) {
            return root;
        }
        
        for (let child of root.children) {
            const found = this.findNodeByName(child, name);
            if (found) {
                return found;
            }
        }
        
        return null;
    }

    start() {
        this.exitBtn.on(Button.EventType.CLICK, this.onExitGame, this);
        this.settingBtn.on(Button.EventType.CLICK, this.onOpenSetting, this);
        this.pauseBtn.on(Button.EventType.CLICK, this.onPauseGame, this);
    }

    update(deltaTime: number) {
        
    }

    onExitGame()
    {
        resources.releaseUnusedAssets();

        director.loadScene("loading")
    }

    onPauseGame()
    {
        if(director.isPaused())
        {
            director.resume();
            return;
        }
        director.pause();
    }

    onOpenSetting()
    {
        UIManager.instance.openDialog("UISettings")
    }
}

